import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
from tqdm import tqdm
import pickle

# 加载手写数字数据集
digits = datasets.load_digits()
X, y = digits.data, digits.target

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 初始化变量
best_accuracy = 0
best_k = 0
accuracies = []

# 尝试不同的K值（1～40）
for k in tqdm(range(1, 41), desc="Finding best K value"):
    knn = KNeighborsClassifier(n_neighbors=k)
    knn.fit(X_train, y_train)
    y_pred = knn.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    accuracies.append(accuracy)
    
    # 更新最优准确率和对应的K值
    if accuracy > best_accuracy:
        best_accuracy = accuracy
        best_k = k
        best_knn = knn

# 打印最优准确率和对应的K值
print("Best accuracy: {:.2f}%".format(best_accuracy * 100))
print("Best K value:", best_k)

# 绘制准确率变化图
plt.plot(range(1, 41), accuracies, marker='o')
plt.axvline(x=best_k, color='r', linestyle='--')

plt.text(best_k + 0.5, best_accuracy - 0.01, f'Acc={best_accuracy:.2f}', verticalalignment='bottom', horizontalalignment='left')


plt.xlabel('K value')
plt.ylabel('Accuracy')
plt.title('K value vs Accuracy')
plt.savefig('accuracy_plot.pdf')
plt.show()

# 保存最优的KNN模型到pickle文件
with open('best_knn_model.pkl', 'wb') as f:
    pickle.dump(best_knn, f)
